package vis.core.common.valid.base;

import vis.core.common.util.SpringKit;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 枚举校验器
 */
@Aspect
@Component
public class ValidBaseAspect {

    @Before("@annotation(vis.core.common.valid.base.Valid)")
    public void valid(JoinPoint joinPoint) throws Exception {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //参数名
        String[] paramNameList = methodSignature.getParameterNames();
        //参数值
        Object[] paramValueList = joinPoint.getArgs();

        //遍历参数，获取要处理的注解
        for (int i = 0; i < paramValueList.length; i++) {
            String paramName = paramNameList[i];
            Object paramValue = paramValueList[i];

            // 获取参数上的注解
            Annotation[] parameterAnnotation = methodSignature.getMethod().getParameterAnnotations()[i];

            for (Annotation annotation : parameterAnnotation) {
                Class<? extends Annotation> annotationType = annotation.annotationType();
//                System.out.println("Annotation type: " + annotationType.getName());
//                System.out.println("Annotation: " + annotation.annotationType().getName());

                // 获取注解上的其他注解
                Annotation[] nestedAnnotations = annotation.annotationType().getAnnotations();
                for (Annotation nestedAnnotation : nestedAnnotations) {

                    //找到具有  ValidBy 处理类的注解
                    if (nestedAnnotation.annotationType().equals(ValidBy.class)) {
//                        System.out.println("存在 ValidBy注解：" + nestedAnnotation.annotationType().getName());
                        Method[] methods = nestedAnnotation.annotationType().getDeclaredMethods();

                        for (Method method : methods) {
                            //ValidBy 注解中 对应的实现类 class
                            String className = method.invoke(nestedAnnotation).toString().substring((6));
                            Class<?> clazz = Class.forName(className);
                            ValidBaseHandler bean = (ValidBaseHandler) SpringKit.getBean(clazz);
                            bean.valid(annotation, paramName, paramValue);
                            //                                Object instance = clazz.newInstance();
                            //                                System.out.println("反射后Class类" + clazz);
                            //                                Method methodFs = clazz.getMethod("valid", String.class, Object.class);
                            //                                methodFs.invoke(instance, , );

                        }
                    }

                }
            }
        }
    }
}
//获取需要解析的注解(怎么知道从中选出想要的注解，反射处理器，进行处理)
//            ValEnum annotation = (ValEnum) Arrays.stream(parameterAnnotation)
//                    .filter(a -> a.annotationType().equals(ValEnum.class))
//                    .findFirst()
//                    .orElse(null);

// 反射处理器，进行处理)
//            if (annotation != null) {
//                String[] value = annotation.value();
//                System.out.println("Parameter " + paramName + ": " + paramValue);
//            }